package com.rzgeo.workmap;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.rzgeo.rzmaplib.actv.RmlEntranceActv;
import com.rzgeo.rzmaplib.model.RmlEnv;

public class HelloActv extends RmlEntranceActv {

    private EditText _tinUsername;
    private EditText _tinPass;
    private Button _btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actv_hello);

        _tinUsername= findViewById(R.id.tin_username);
        _tinPass= findViewById(R.id.tin_pass);
        _btnLogin=findViewById(R.id.btn_login);

        _btnLogin.setEnabled(super.isLoaded());
        _btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptLogin();
            }
        });

    }

    @Override
    protected String getAppName() {
        return "HnpgcMap";
    }

    @Override
    protected void onInitialized(boolean success, String msg) {
        if(_btnLogin!=null)
            _btnLogin.setEnabled(success);
    }
    private void showProgress(boolean show) {

    }

    private void attemptLogin() {
        String userName = _tinUsername.getText().toString();
        String pass = _tinPass.getText().toString();

        showProgress(true);
        new UserLoginTask(userName,pass).execute((Void) null);
    }


    public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String mEmail;
        private final String mPassword;

        UserLoginTask(String email, String password) {
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.

            try {
                // Simulate network access.
                Thread.sleep(400);
            } catch (InterruptedException e) {
                return false;
            }

            return true;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
//            mAuthTask = null;
            showProgress(false);

            if (success) {
                Intent intent=new Intent();
                intent.putExtra(
                        CatalogActv.StartInfo.Key,
                        new CatalogActv.StartInfo(mEmail)
                );

                intent.setClass(HelloActv.this,CatalogActv.class);
                startActivity(intent);
//                finish();
            } else {
//                mPasswordView.setError(getString(R.string.error_incorrect_password));
//                mPasswordView.requestFocus();
            }
        }

        @Override
        protected void onCancelled() {
            showProgress(false);
        }
    }
}
